angular.module("app").controller("welfare.info.Ctrl", ["$scope", "$stateParams", "welfare","$timeout","$filter", function ($scope, $stateParams, welfare,$timeout,$filter) {
    $scope.provinceChange = function(){
        var provinceId = $scope.province.filter(function (item) {
            return item.ProName == $scope.welfareInfo.province;
        })[0].ProID;
        $scope.currCityList = city.filter(function (item) {
            return item.ProID == provinceId;
        });
    };
    if ($stateParams.welfareInfo) {
        $scope.welfareInfo = $stateParams.welfareInfo;
        $scope.welfareInfo.startDay = new Date($scope.welfareInfo.startDay);
        $scope.welfareInfo.endDay = new Date($scope.welfareInfo.endDay);
        $scope.welfareInfo.useStart = new Date($scope.welfareInfo.useStart);
        $scope.welfareInfo.useEnd = new Date($scope.welfareInfo.useEnd);
        try {
            $scope.welfareInfo.useDemo = JSON.parse($scope.welfareInfo.useDemo);
        }
        catch (e) {
            $scope.welfareInfo.useDemo = [];
        }
        if($scope.welfareInfo.area != "全国"){
            var tmp = $scope.welfareInfo.area.split(";");
            $scope.welfareInfo.province = tmp[0];
            $scope.welfareInfo.city = tmp[1];
            $scope.provinceChange();
            welfareInfo.area = "定向";
        }
    }
    else if ($stateParams.id) {
        // welfare.getUserInfo({}, { id: $stateParams.id }, function (data) {
        //     if (data.statusCode == 0) {
        //         $scope.manageInfo = data.data;
        //         $scope.manageInfo.roles.split(",").forEach(function (item) {
        //             var tmp = $scope.roleDict[item.replace(/[^\d]+/g, "")];
        //             if (tmp) {
        //                 tmp.checked = true;
        //             }
        //         });
        //     } else {
        //         alert(data.statusMessage)
        //     }
        // })
    }
    else {
        $scope.welfareInfo = { area: "全国", delStatus: 0, status: 0, type: 1, code: "0" };
        // $scope.provinceChange();
    }

    $scope.type = $stateParams.type;
    $scope.types = { create: "创建", edit: "编辑", view: "查看" };
    $scope.startDayConfig = {
        formatString: "yyyy-MM-dd HH:mm:ss",
        format: "datetime",
        bindingOptions: {
            value: "welfareInfo.startDay"
        }
    };
    $scope.endDayConfig = {
        formatString: "yyyy-MM-dd HH:mm:ss",
        format: "datetime",
        bindingOptions: {
            value: "welfareInfo.endDay"
        }
    };
    $scope.useStartConfig = {
        formatString: "yyyy-MM-dd HH:mm:ss",
        format: "datetime",
        bindingOptions: {
            value: "welfareInfo.useStart"
        }
    };
    $scope.useEndConfig = {
        formatString: "yyyy-MM-dd HH:mm:ss",
        format: "datetime",
        bindingOptions: {
            value: "welfareInfo.useEnd"
        }
    };
    $scope.addItem = function () {
        if (!$scope.welfareInfo.useDemo) {
            $scope.welfareInfo.useDemo = [];
        }
        var item = { type: "text", sort: $scope.welfareInfo.useDemo.length || 0 };
        $scope.welfareInfo.useDemo.push(item);
    };
    $scope.removeItem = function (currItem) {
        if (confirm("确定执行该操作吗?")) {
            $scope.welfareInfo.useDemo.forEach(function (item, index) {
                if (currItem.sort == item.sort) {
                    $scope.welfareInfo.useDemo.splice(index, 1)
                }
            })
        }
    };
    $scope.saveWelfareInfo = function () {
        if(!$scope.welfareInfo.province.length){
            alert("请选择正确兑换区域!")
            return;
        }
        var welfareInfo = angular.copy($scope.welfareInfo);
        if(welfareInfo.area == "定向"){
            welfareInfo.area = welfareInfo.province + ";" +(welfareInfo.city || "")
        }
        welfareInfo.startDay = $filter("date")(welfareInfo.startDay,"yyyy-MM-dd HH:mm:ss");
        welfareInfo.endDay = $filter("date")(welfareInfo.endDay,"yyyy-MM-dd HH:mm:ss");
        welfareInfo.useStart = $filter("date")(welfareInfo.useStart,"yyyy-MM-dd HH:mm:ss");
        welfareInfo.useEnd = $filter("date")(welfareInfo.useEnd,"yyyy-MM-dd HH:mm:ss");
        delete welfareInfo.province;
        welfare.saveWelfare({}, welfareInfo, function (data) {
            if (data.statusCode == 0) {
                alert("更新成功!");
                $scope.goBack();
            } else {
                alert(data.statusMessage)
            }
        })
    };
    
    $scope.uploadPhoto = function(file,item){
        var form = new FormData();
        form.append("file", file);
        form.append("key", new Date().getTime() + /\.[^\.]+$/.exec(file.name)[0]);
        form.append("token", $scope.$parent.qiniuToken);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://upload.qiniu.com/", true);
        function callback() {
            if (xhr.readyState == 4) {
                $timeout(function() {
                    var data = angular.fromJson(xhr.responseText);
                    if (data.key) {
                        item.img = "http://img.dakaqi.cn/" + data.key;
                    } else if (data.code) {
                        alert(data.error);
                        return;
                    }
                }, 3000);
            }
        }
        xhr.onreadystatechange = callback;
        xhr.send(form);
    }
}]);